﻿@model MasterDataIndexModel
@if (ViewBag.IsPjaxRequest != null && ViewBag.IsPjaxRequest == false)
{
    Layout = "../Shared/__Layout.cshtml";
}
<section class="content-header">
    <h1>
        主数据设置
    </h1>
    <ol class="breadcrumb">
        <li><a href="@Url.Action("Index", "Home")"><i class="fa fa-home"></i> 首页</a></li>
        <li class="active">系统设置</li>
    </ol>
</section>
<div class="content">
    <div class="row">

        <div class="col-lg-3 col-md-6" style="  height:100%">
            <div class="box box-primary">
                <div class="box-header with-border">
                    <div class="dataTable-tools btn-group">
                        @Html.Bootstrap().LinkButton("新增", ToolBarIco.Create).Click("window.app.showAddWindow('" + Url.Action("CategoryCreate") + "',this,{}, event);").Attribute("data-toggle", "ajaxModal").Attribute("data-search", "#leftCategory").Attribute("role", "createbutton")
                        @Html.Bootstrap().LinkButton("修改", ToolBarIco.Edit).Click("window.app.showEditWindow('" + Url.Action("CategoryEdit") + "',this,{},event);").Attribute("data-toggle", "ajaxModal").Attribute("data-search", "#leftCategory").Attribute("role", "editbutton")
                        @Html.Bootstrap().LinkButton("删除", ToolBarIco.Delete).Click("window.app.deleteItem('dataList','" + Url.Action("CategoryDelete") + "',this);").Attribute("data-search", "#leftCategory").Attribute("role", "deletebutton")
                    </div>
                </div>

                <div class="table-responsive">
                    <table id="leftCategory" width="100%" class="table table-bordered table-striped table-hover dataTable text-center">
                    </table>
                </div>
            </div>

        </div>
        <div class="col-lg-9 col-md-6" style="height:100%;">
            <div class="box box-primary">
                @*<header class="panel-heading title">
                        主数据设置
                    </header>*@
                <div class="box-header with-border">
                    <div class="dataTable-tools btn-group">
                        @Html.Bootstrap().LinkButton("添加", ToolBarIco.Create).Id("masterDataCreate").Click("javascript:alert('请选择分类');").Route(Url.Action("Create")).Attribute("data-search", "#masterDataList")
                        @Html.Bootstrap().LinkButton("删除", ToolBarIco.Delete).Click("DeleteMasterData('masterDataList','" + @Url.Action("Delete") + "')").Route(Url.Action("Delete")).Attribute("data-search", "#masterDataList")
                    </div>
                    <div class="table-responsive">
                        <table id="masterDataList" width="100%" class="table table-bordered table-striped table-hover dataTable text-center"></table>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>
<input id="category" name="category" type="hidden" value="" />


<script type="text/javascript">


    function leftCategoryClick(o) {

        $("#category").val($(o).data("code"));
        $("#masterDataCreate").attr("href","@Url.Action("Create")/?category="+escape($("#category").val()));
        $("#masterDataCreate").attr("onclick", "");
        $("#masterDataCreate").attr("data-toggle","ajaxModal");

        var table= $('#masterDataList').DataTable();

        table.ajax.reload();

    }
    $(document).ready(function(){
        LoadData();
    });


    function LoadData() {
        $('#masterDataList').dataTable({
            serverSide: true,
            sort: false,
            displayLength: 14, 
            select: { style: 'multi', selector: 'td:first-child' },
            "columnDefs": [{ sDefaultContent: '', aTargets: ['_all'] }],
            ajax: {
                "url": "@Url.Action("GetPaged")",
                "type": "POST"
            },
            serverParams: function (aoData) {
                eval("aoData.category='" + $("#category").val() + "'");
            },
            columns: [
                { title: '', data: null, sortable: false, width: 30, "sClass": 'checkColumn select-checkbox' },
                { title: "代码", data: 'code', width: 120, "sClass": "center" },
                {
                    title: "名称", width: 160, data: function (row) { return '<a data-toggle="ajaxModal" class="auto" href="@Url.Action("Edit")/?id=' + row.id + '&category=' + row.category +'&ListControlId=masterDataList">'+row.name+'</a>'; }
                },
                { title: "全称", data: 'fullName' }

            ]
        });

        $('#leftCategory').dataTable({
            serverSide: true,
            sort: false,
            displayLength: 14, paging: false, info: false,
            select: { style: 'multi', selector: 'td:first-child' },
            "columnDefs": [{ sDefaultContent: '', aTargets: ['_all'] }],
            ajax: {
                "url": "@Url.Action("GetCategoryPaged")",
                "type": "POST"
            },
            columns: [
                { title: '', data: null, sortable: false, width: 30, "sClass": 'checkColumn select-checkbox' },
                { title: "代码", width: 120, "sClass": "center", data: function (row) { return '<a data-code="' + row.code +'" href="javascript:void(0);" onclick="leftCategoryClick(this)">'+row.name+'</a>'; }}
            ]
        });
    }

    function DeleteMasterData(dataTable, url) {

        if ($("#" + dataTable).length === 0) {
            window.app.message("实现错误，列表Id给错。");
            return;
        }
        var ids = new Array();

        var list = $("#" + dataTable).DataTable().rows({ selected: true }).data();
        $.each(list, function (index, item) {
            ids.push(item.id);
        });

        if (ids.length === 0) {
            window.app.message("请选中一项。");
            return;
        }

        swal({
            title: "请确认是否删除？",
            text: "您正在进行删除操作，该操作不可逆!",
            type: "warning",
            showCancelButton: true,
            confirmButtonClass: "btn-danger"
        }).then(function(isConfirm) {
            if (isConfirm === true){
                $.ajax({
                    type: "POST",
                    url: url,
                    data: {
                        "ids": ids,
                        category : $("#category").val()
                    },
                    dataType: "json",
                    success: function (data) {
                        if (data.errors.length > 0) {
                            swal("出错啦！", data.errors, "error");
                        }
                        else {
                            swal("已经删除", "你已经删除成功！", "success");
                            $('#masterDataList').DataTable().ajax.reload();
                        }
                    }
                });
            }
        });
    }
</script>